package com.gt.stream

import org.apache.spark.SparkConf
import org.apache.spark.streaming.dstream.{DStream, ReceiverInputDStream}
import org.apache.spark.streaming.{Durations, Seconds, StreamingContext}

object Streaming_WC_02 {

  def main(args: Array[String]): Unit = {

    //1. 创建上下文对象
    val conf: SparkConf = new SparkConf().setAppName("xx").setMaster("local[*]")
    val ssc = new StreamingContext(conf, Durations.seconds(1))

    //2. 读取数据
    val hostName = "localhost"
    val port = 9999
    val srcStream: ReceiverInputDStream[String] = ssc.socketTextStream(hostName, port)

    /**
     * 窗口最小批次时间 : 1s
     * 窗口大小        : 5s
     * 滑动时间        : 3s
     * 每3s计算一次前5秒的数据
     */
    val dStream: DStream[String] = srcStream.window(Seconds(5), Seconds(3))
    val resultStream: DStream[(String, Int)] = dStream.flatMap(_.split(" "))
      .map((_, 1))
      .reduceByKey(_ + _)

    resultStream.print()


    ssc.start()
    ssc.awaitTermination()
  }

}
